<?php
namespace Api;
use Kuyuan\WqCore\api\Api;
use Kuyuan\WqCore\util\File;
use Kuyuan\WqCore\util\Util;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

/**
 * 万能表单
 * Class Link
 * @package Api
 */
class Form extends Api
{
    private $Form = null;

    public function __construct()
    {
        parent::__construct();
        $this->Form = new \Controller\Form();
    }

    /**
     * 新增编辑表单
     */
    public function edit()
    {
        Util::appointPost();
        if ($_form_id = $this->Form->edit($this->_gpc["data"], $this->_gpc["form_id"])) {
            $_result = array("code" => 0, "data" => $_form_id);
        } else {
            $_result = array("code" => 103);
        }
        Util::returns($_result);
    }

    /**
     * 获取用户万能表单列表
     */
    public function getUserAll()
    {
        Util::appointGet();
        $_list = $this->Form->getUserAll(
            $this->_gpc["limit"],
            0
        );
        if (count($_list) > 0) {
            $_result = array("code" => 0, "data" => $_list);
        } else {
            $_result = array("code" => 101);
        }
        Util::returns($_result);
    }

    /**
     * 获取一个表单详情
     */
    public function get()
    {
        Util::appointGet();
        $_data = $this->Form->get($this->_gpc["form_id"]);
        if ($_data) {
            $_result = array("code" => 0, "data" => $_data);
        } else {
            $_result = array("code" => 100);
        }
        Util::returns($_result);
    }

    /**
     * 删除表单
     */
    public function del()
    {
        Util::appointPost();
        if ($this->Form->del($this->_gpc["form_id"])) {
            $_result = array("code" => 0);
        } else {
            $_result = array("code" => 102);
        }
        Util::returns($_result);
    }

    /**
     * 提交表单
     */
    public function submit()
    {
        Util::appointPost();
        $_form = $this->Form->get($this->_gpc["form_id"]);
        $_form_data = $_form["form_data"];
        unset($_form);
        $_form_content = array();
        for ($i = 0; $i < count($_form_data); $i++) {
            $_temp_value = $this->_gpc["data"]["form_" . $i];
            $_form_content[$i] = array(
                "label" => $_form_data[$i]["label"],
                "value" => is_array($_temp_value) ? implode("，", $_temp_value) : $_temp_value
            );
        }
        unset($_temp_value);
        $RedForm = $this->newController("RedForm");
        $_result = $RedForm->add(
            array(
                "red_id" => $this->_gpc["red_id"],
                "form_content" => $_form_content
            )
        );
        if ($_result) {
            $_result = array("code" => 0);
        } else {
            $_result = array("code" => 103);
        }
        Util::returns($_result);
    }

    /**
     * 获取表单列表
     */
    public function getList()
    {
        Util::appointGet();
        $RedForm = $this->newController("RedForm");
        $_data = $RedForm->getList($this->_gpc["red_id"],$this->_gpc["limit"]);
        if($_data) {
            $Member = $this->newController("Member");
            foreach($_data as $_k=>$_v) {
                $_data[$_k]["user"] = $Member->findNameHeader($_v["uid"]);
            }
            unset($RedForm,$Member);
            $_result = array("code"=>0,"data"=>$_data);
        } else {
            $_result = array("code"=>101);
        }
        Util::returns($_result);
    }

    /**
     * 发送指定红包的表单数据到指定Email
     */
    public function sendEmail()
    {
        Util::appointPost();
        $RedForm = $this->newController("RedForm");
        $_data = $RedForm->getList($this->_gpc["red_id"]);
        $_excel_data_title = array_column($_data[0]["form_content"],"label");
        $Spreadsheet = new Spreadsheet();
        $sheet = $Spreadsheet->getActiveSheet();
        $_title_col = 'A';
        foreach($_excel_data_title as $_v) {
            $sheet->setCellValue($_title_col."1",$_v);
            $_title_col++;
        }
        $_row = 2;
        foreach($_data as $_item) {
            $_data_col = "A";
            foreach($_item["form_content"] as $_value) {
                $sheet->setCellValue($_data_col.$_row," ".$_value["value"]);
                $_data_col++;
            }
            $_row++;
        }
        $Xlsx = new Xlsx($Spreadsheet);
        $_excel_filename = ATTACHMENT_ROOT.File::path('excel','xlsx');
        $Xlsx->save($_excel_filename);


        $_body = array(
            '红包表单数据',
            '@'.$_excel_filename
        );
        load()->func('communication');
        $_send_result = @ihttp_email($this->_gpc["email"],'红包表单数据',$_body);
        if($_send_result["errno"]) {
            $_result = array("code"=>900,"err_msg"=>"发送失败");
        } else {
            $_result = array("code"=>0);
        }
        unlink($_excel_filename);
        Util::returns($_result);
    }
}